package cn.im.qywx.wx.component.wakeon;

import cn.im.qywx.model.ImWxUserInfo;
import cn.im.qywx.queue.UserQueue;
import cn.im.qywx.system.SystemParams;
import cn.im.qywx.wx.component.Component;
import cn.im.qywx.wx.component.ComponentType;
import cn.im.qywx.wx.component.wakeon.util.UdpClientSocket;
import cn.im.qywx.wx.util.WriteWeiXinMsgXml;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description: 远程开机服务
 */
@Slf4j
public class WakeOnCom extends Component {
	

	@Override
	public void execute(ImWxUserInfo userInfo ) {
		
		String pubStr = null;
		String recvMsg = null;
		String sendMsg = null;
		if(UserQueue.getUsersMap().keySet().contains(userInfo.getWeixinno())){
			sendMsg = "wol-01 "+ userInfo.getWeixinno()+" open";
			 try {
			    	UdpClientSocket.getInstance().send(SystemParams.UDP_IP, Integer.valueOf(SystemParams.UDP_PORT), sendMsg.getBytes());
			    	recvMsg = UdpClientSocket.getInstance().receive(SystemParams.UDP_IP, Integer.valueOf(SystemParams.UDP_PORT));
			    	log.info("开机请求:{}  已发送至：{}， 服务端回应数据：{}",sendMsg,SystemParams.UDP_IP+":"+SystemParams.UDP_PORT, recvMsg);
				} catch (Exception e) {
					pubStr = "开机请求发送失败";
					log.error("exception：" + pubStr + "  sendMsg:"+ sendMsg);
					e.printStackTrace();
				}
			    
			    if(recvMsg != null){
			    	if(recvMsg.equalsIgnoreCase("Yes, My lord.")){
			    		pubStr = "开机请求发送成功";
			    	} if(recvMsg.equalsIgnoreCase("fail")){
			    		pubStr = "指令错误，请联系管理员！";
			    	}
			    }else{
			    	pubStr = "开机请求发送失败";
			    }
		}else{
			pubStr = "请联系管理员开通使用权限";
		}
	   
	        
		
		super.setParams(userInfo, ComponentType.MainMenu,  pubStr, new WriteWeiXinMsgXml());
	}
}